package com.bytz.modules.cms.reseller.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.reseller.entity.EndUser;
import com.bytz.modules.cms.reseller.model.EndUserPage;
import com.bytz.modules.cms.reseller.service.IAdminEndUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "终端用户")
@RequestMapping("/endUser")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.EndUser.EDIT + "')")
public class EndUserController extends BytzController<EndUser, IAdminEndUserService> {

    @Autowired
    private IAdminEndUserService adminEndUserService;

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<EndUser> queryById(@RequestParam(value = "id") String id) {

        return Result.ok(adminEndUserService.getById(id));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<EndUserPage<EndUser>> page(EndUser endUser,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {

        return Result.ok(adminEndUserService.queryPage(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initLambdaQueryWrapper(endUser, req.getParameterMap())
        ));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {
        ExcelUtil.export("导入客户", EndUser.class, response, adminEndUserService.list());
    }

    @ApiOperation(value = "导入用户", notes = "导入用户")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {

        List<EndUser> models = ExcelUtil.importGroup(request, EndUser.class, null);
        adminEndUserService.mergeDb(models);
        return Result.ok();
    }

    @ApiOperation(value = "同步用户", notes = "同步用户")
    @RequestMapping(value = "/sync", method = RequestMethod.POST)
    public Result<Boolean> sync() {
        return Result.ok(adminEndUserService.sync());
    }

    @ApiOperation(value = "根据编码搜索", notes = "根据编码搜索")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Result<List<EndUser>> search(@RequestParam String numberLike) {
        return Result.ok(adminEndUserService.search(numberLike));
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(adminEndUserService.checkAndRemoveById(id));
    }

}
